
import { _decorator, AnimationClip, Animation, Component, Node, SpriteFrame, UITransform, Sprite, color, Color } from 'cc';
import { Loader } from '../../tools/LoadManger';
import { monster_base_config } from '../../manager/AppControll';
import { resManger } from '../../tools/ResManger';
const { ccclass, property } = _decorator;


@ccclass('MAnimation')
export class MAnimation extends Component {
    private strAnPath = null;
    private ltRunAnSpfs: SpriteFrame[] = null;
    private ltDeathAnSpfs: SpriteFrame[] = null
    private ndAnimation: Node = null;
    private mBIsDeath = false;

    protected onLoad(): void {
        this.ndAnimation = this.node.getChildByName("Ani");
    }

    BeAttack() {
        this.ndAnimation.getComponent(Sprite).color = new Color(10, 255, 255, 255);
        this.scheduleOnce(() => {
            this.ndAnimation.getComponent(Sprite).color = new Color(255, 255, 255, 255);
        }, 0.1);
    }

    InitMAnimation(path: string, config: monster_base_config) {
        if (!this.ndAnimation) {
            this.ndAnimation = this.node.getChildByName("Ani");
        }
        this.strAnPath = path;
        let an1: Animation = this.ndAnimation.getComponent(Animation);
        if (!an1) an1 = this.ndAnimation.addComponent(Animation);
        an1 = null;

        resManger.GetMonsterAniSpf(this.strAnPath + "/run", (arr) => {
            if (!this.node) return;
            this.ndAnimation = this.node.getChildByName("Ani");
            let an: Animation = this.ndAnimation.getComponent(Animation);
            if (!an) an = this.ndAnimation.addComponent(Animation);
            this.ltRunAnSpfs = arr;
            const clip = AnimationClip.createWithSpriteFrames(this.ltRunAnSpfs, this.ltRunAnSpfs.length);
            clip.name = "run";
            clip.wrapMode = AnimationClip.WrapMode.Loop;
            clip.speed = config.runAniSpeed;
            an.addClip(clip);
            this.PlayRun();
            an = null;
        })
        resManger.GetMonsterAniSpf(this.strAnPath + "/death", (arr) => {
            if (!this.node) return;
            this.ndAnimation = this.node.getChildByName("Ani");
            let an: Animation = this.ndAnimation.getComponent(Animation);
            if (!an) an = this.ndAnimation.addComponent(Animation);
            this.ltDeathAnSpfs = arr;
            const clip = AnimationClip.createWithSpriteFrames(this.ltDeathAnSpfs, 8);
            clip.name = "death";
            clip.wrapMode = AnimationClip.WrapMode.Normal;
            clip.speed = config.deathAniSpeed;
            an.addClip(clip);
            an = null;
        })
    }

    PlayRun() {
        this.unschedule(this.PlayRun);
        this.ndAnimation.getComponent(Animation).play("run")
    }

    PlayDeath(callback: () => void) {
        this.mBIsDeath = true;
        this.ndAnimation.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.ndAnimation.getComponent(Animation).stop();
            this.ndAnimation.getComponent(Animation).destroy();
            if (callback) callback();
        })
        this.ndAnimation.getComponent(Animation).play("death")
    }

    StopAll() {
        let an: Animation = this.ndAnimation.getComponent(Animation);
        if (an) {
            an.stop();
            an.clips = [];
        }
        an = null;
    }
}
